package com.zzx.schoolfellow.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzx.schoolfellow.common.api.ApiResult;
import com.zzx.schoolfellow.mapper.BmsPromotionMapper;
import com.zzx.schoolfellow.model.entity.BmsPromotion;
import com.zzx.schoolfellow.model.entity.BmsTag;
import com.zzx.schoolfellow.model.entity.UmsUser;
import com.zzx.schoolfellow.service.IBmsPromotionService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * @program: schoolfeoolw_backend
 * @description:
 * @author: zzx
 * @create: 2023-03-14 21:58
 **/
@RestController
@RequestMapping("/promotion")
public class BmsPromotionController extends BaseController {

    @Resource
    private IBmsPromotionService bmsPromotionService;
    @Resource
    private BmsPromotionMapper bmsPromotionMapper;

    @GetMapping("/all")
    public ApiResult<List<BmsPromotion>> list() {
        List<BmsPromotion> list = bmsPromotionService.list();
        return ApiResult.success(list);
    }

    @GetMapping("/list")
    public List<BmsPromotion> getUserList(@RequestParam("keyword") String keyword) {
        List<BmsPromotion> list;
        if (Objects.equals(keyword, "")) {
            list = bmsPromotionService.list();
        } else {
            list = bmsPromotionService.list(new QueryWrapper<BmsPromotion>()
                    .like("title", keyword)
                    .or().like("description", keyword));
        }
        return list;
    }

    @DeleteMapping("/delete/{id}")
    public ApiResult<String> deleteTag(@PathVariable("id") String id) {
        bmsPromotionService.removeById(id);
        return ApiResult.success(null, "删除成功");
    }

    @PostMapping("/update")
    public ApiResult<BmsPromotion> updatePromote(@Valid @RequestBody BmsPromotion name) {
        bmsPromotionService.updateById(name);
        return ApiResult.success(name);
    }

    @PostMapping("/add")
    public ApiResult<BmsPromotion> add(@Valid @RequestBody BmsPromotion name) {
        bmsPromotionMapper.insert(name);
        return ApiResult.success(name);
    }

}
